#!/usr/bin/perl -w

#########################

my $froglog="/home/frogd/var/frogd.log";
my $id="redge76";
my $password="regis1976";

#########################


use strict;
use Socket;
use IO::Handle;
use POSIX qw(strftime);

sub contact_server {

    my ($ID,$PASSWORD,$dateutc,$baromin,$tempf,$humidity,$winddir,$windspeedmph,$windgustmph,$rainin,$dewptf,$weather,$clouds)=@_;

    my ($line, $ip, $pass, %result, %arg, $command);
    
    socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or
        die "socket: $!";
    $ip = inet_aton("weather.weatherbyu.com") or die "host: $!";
    connect(SOCK, sockaddr_in(80, $ip)) or die "connect: $!";
    
    autoflush SOCK 1;
    
    $command = "GET http://weather.weatherbyu.com/WeatherUnderground?"
	."ID=$ID"
	."&PASSWORD=$PASSWORD"
	."&dateutc=$dateutc"
	."&winddir=$winddir"
	."&windspeedmph=$windspeedmph"
        ."&windgustmph=$windgustmph"

        ."&tempf=$tempf"
        ."&rainin=$rainin"
        ."&baromin=$baromin"
        ."&dewptf=$dewptf"
        ."&humidity=$humidity"
        ."&weather=$weather"
        ."&clouds=$clouds"
	."&softwaretype=FrogUnderground"
	."&action=updateraw"
	." HTTP/1.0\r\n";

    print $command;

    print SOCK $command;
    print SOCK "User-Agent: FrogUnderground 0.0a1`r\n";
    print SOCK "Host: weatherstation.wunderground.com\r\n";
    print SOCK "\r\n";
    
    while(<SOCK>) {

	print $_;

    }
    
    close(SOCK);
}


########################################### MAIN #############################################


my $date= strftime "%Y-%m-%d%%20%H%%3A%M%%3A%S", gmtime;

open(FH,$froglog) || die "unable to open frogd logfile $froglog\n";
my @lignes = <FH>;
my $l=$lignes[$#lignes];
chop($l);
my @element = split(/:/, $l);

my $barohPa=$element[3];
my $temp=$element[2];
my $hum=$element[4];

my $baroin=$barohPa*0.02953;
my $tempf=$temp*9/5+32;

contact_server($id,$password,$date,$baroin,$tempf,$hum,"","","","","","","");
#contact_server($id,$password,$date,$barohPa,$temp,$hum,"","","","","","","");

